<template>
  <!-- 员工照片组件 -->
  <div>
    <el-upload
      action="#"
      list-type="picture-card"
      :limit="1"
      :file-list="filelist"
      :on-preview="onpreview"
      :class="{disabled:fileCount>0}"
      :on-remove="onRemove"
      :on-change="onChange"
      :before-upload="beforeupload"
      :http-request="upload"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <!-- 进度条 -->
    <el-progress v-if="showPercent" style="width: 180px" :percentage="percent" />
    <!-- 预览对话框 -->
    <el-dialog :visible.sync="dialogVisible" width="50%" title="图片预览">
      <img width="100%" :src="url" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
var cos = new COS({
  SecretId: 'AKIDmRs79zf9OPCm9IFxQefT3NSJwLVQ6Xkj',
  SecretKey: 'mKekcFdmhMnzwf9OQCk4A1d4GEyTjlZ8'
})
export default {
  name: 'UploadImage',
  components: {},
  props: {},
  data() {
    return {
      filelist: [], // 上传图片信息
      dialogVisible: false, // 对话框展示状态
      url: '',
      currentUid: undefined,
      showPercent: false, // 进度条状态
      percent: 0// 上传进度
    }
  },
  computed: {
    fileCount() {
      return this.filelist.length
    }
  },
  created() {

  },
  methods: {
    // 图片预览
    onpreview(feil) {
      console.log(feil)
      this.url = feil.url
      this.dialogVisible = true
    },
    // 删除图片
    onRemove(file) {
      const index = this.filelist.findIndex(item => item.uid !== file.uid)
      this.filelist.splice(index, 1)
    },
    // 修改图片
    onChange(file, fileList) {
      this.filelist = fileList.map(item => item)
    },
    // 图片上传前的验证
    beforeupload(file) {
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      const maxSize = 5 * 1024 * 1024
      if (!types.includes(file.type)) {
        this.$message({
          message: '上传图片只能是 JPG、GIF、BMP、PNG 格式!',
          type: 'error',
          showClose: true,
          duration: 3000
        })
        return false
      }
      //  检查大小
      if (maxSize < file.size) {
        this.$message.error('图片大小最大不能超过5M')
        return false
      }
      this.showPercent = true
      this.currentUid = file.uid
      return true
    },
    // 上传图片
    upload(params) {
      if (params.file) {
        // 执行上传操作
        cos.putObject({
          Bucket: 'mjfyl-1306752985', // 存储桶
          Region: 'ap-chengdu', // 地域
          Key: params.file.name, // 文件名
          Body: params.file, // 要上传的文件对象
          StorageClass: 'STANDARD', // 上传的模式类型 直接默认 标准模式即可
          // 上传到腾讯云 =》 哪个存储桶 哪个地域的存储桶 文件  格式  名称 回调
          onProgress: (params) => {
            this.percent = params.percent * 100
          }// 获取上传进度的回调函数
        }, (err, data) => {
          // data返回数据之后 应该如何处理
          console.log(err || data)
          if (!err && data.statusCode === 200) {
            const f = this.filelist.find(t => t.uid === this.currentUid)
            if (f) {
              f.url = 'http://' + data.Location
              f.upload = true
            } else {
              this.filelist.push({ url: 'http://' + data.Location, upload: true })
            }
            this.showPercent = false
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .disabled .el-upload--picture-card{
    display: none;
}
</style>
